package com.cytech.cloudclinic.moudle.main.setting.prescription.group;

import android.content.Context;
import android.content.Intent;
import android.view.View;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.entity.node.BaseNode;
import com.chad.library.adapter.base.listener.OnItemChildClickListener;
import com.cytech.cloudclinic.R;
import com.cytech.cloudclinic.adapter.TemplateAdapter;
import com.cytech.cloudclinic.adapter.provide.FirstNode;
import com.cytech.cloudclinic.adapter.provide.SecondNode;
import com.cytech.cloudclinic.base.BaseActivity;
import com.cytech.cloudclinic.bean.PrescriptionTag;
import com.yechaoa.yutils.ToastUtil;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.OnClick;

/**
 * 处方分组管理
 */
public class PrescriptionGroupActivity extends BaseActivity<PrescriptionGroupPresenter> implements PrescriptionGroupView {

    @BindView(R.id.titlebar_title)
    TextView tvTitle;
    @BindView(R.id.Group_recy)
    RecyclerView GroupRecy;
    private List<PrescriptionTag> groupList;
    private TemplateAdapter templateAdapter;
    private static int group_drugTagId;

    public static void actionStart(Context context, int drugTagId) {
        Intent intent = new Intent(context, PrescriptionGroupActivity.class);
        context.startActivity(intent);
        group_drugTagId = drugTagId;
    }

    @Override
    protected PrescriptionGroupPresenter createPresenter() {
        return new PrescriptionGroupPresenter(this);
    }

    @Override
    protected int getLayoutId() {
        return R.layout.act_prescription_group;
    }

    @Override
    protected void initView() {
        tvTitle.setText("编辑分组");
        groupList = new ArrayList<>();


    }

    @Override
    protected void initData() {
        presenter.getPrescriptionTags(group_drugTagId);
    }

    @OnClick(R.id.titlebar_back)
    public void exit(View view) {
        finish();
    }


    @Override
    public void getPresciptionListSuccess(List<PrescriptionTag> result) {
        List<BaseNode> menuList = new ArrayList<>();
        for (int i = 0; i < result.size(); i++) {
            //SecondNode 的 list
            List<BaseNode> secondNodeList = new ArrayList<>();
            for (int n = 0; n < result.get(i).diagnoses.size(); n++) {
                SecondNode seNode = new SecondNode(result.get(i).diagnoses.get(n).diagnose,result.get(i).diagnoses.get(n).prescription_id,result.get(i).diagnoses.get(n).prescription_type);
                secondNodeList.add(seNode);
            }

            FirstNode entity = new FirstNode(secondNodeList, result.get(i).value);
            entity.setExpanded(i == 0);
            menuList.add(entity);
        }
        templateAdapter = new TemplateAdapter();
        templateAdapter.setNewInstance(menuList);
        GroupRecy.setLayoutManager(new LinearLayoutManager(this));
        GroupRecy.setAdapter(templateAdapter);
        templateAdapter.setOnItemChildClickListener(new OnItemChildClickListener() {
            @Override
            public void onItemChildClick(@NonNull BaseQuickAdapter adapter, @NonNull View view, int position) {
                ToastUtil.showToast(""+position);
            }
        });

    }
}
